Spring Boot 2.0.0参考手册_中英文对照_Part IV_25

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

25. Profiles

Spring Profiles provide a way to segregate parts of your application configuration and make it only available in certain environments. Any @Component or @Configuration can be marked with @Profile to limit when it is loaded:

Spring Profiles提供了一种隔离部分应用配置的方式,并让这些配置只在特定的环境生效。任何带有@Profile标记的@Component@Configuration在加载时都会受限制:

1
2
3
4
5
6
7
@Configuration
@Profile("production")
public class ProductionConfiguration {

// ...

}

In the normal Spring way, you can use a spring.profiles.active Environment property to specify which profiles are active. You can specify the property in any of the usual ways, for example you could include it in your application.properties:

以正常的Spring方式,你可以使用spring.profiles.active Environment属性来指定激活哪一个profiles。你可以在任何常见的方式指定这个属性,例如你可以在你的application.properties中包含它:

1
spring.profiles.active=dev,hsqldb

or specify on the command line using the switch --spring.profiles.active=dev,hsqldb.

或在命令行用--spring.profiles.active=dev,hsqldb转换。

25.1 Adding active profiles

The spring.profiles.active property follows the same ordering rules as other properties, the highest PropertySource will win. This means that you can specify active profiles in application.properties then replace them using the command line switch.

spring.profiles.active属性与其它的属性一样遵循同样的排序规则,最高的PropertySource优先。这意味着你可以在application.properties指定激活的profiles,然后用命令行转换替代它们。

Sometimes it is useful to have profile-specific properties that add to the active profiles rather than replace them. The spring.profiles.includeproperty can be used to unconditionally add active profiles. The SpringApplication entry point also has a Java API for setting additional profiles (i.e. on top of those activated by the spring.profiles.active property): see the setAdditionalProfiles() method.

有时通过特定的profile属性添加激活的profiles而不替换它们是很有用的。spring.profiles.include属性可以用来无条件添加激活的profiles。SpringApplication入口也有设置额外的profiles的Java API(例如上面的通过spring.profiles.active属性激活的profiles):请看setAdditionalProfiles()方法。

For example, when an application with following properties is run using the switch --spring.profiles.active=prod the proddb and prodmq profiles will also be activated:

例如,当一个应用具有以下属性且运行时使用--spring.profiles.active=prod转换,proddbprodmq profiles也将被激活:

1
2
3
4
5
6
7
---
my.property: fromyamlfile
---
spring.profiles: prod
spring.profiles.include:
- proddb
- prodmq

Remember that the spring.profiles property can be defined in a YAML document to determine when this particular document is included in the configuration. See Section 70.7, “Change configuration depending on the environment” for more details.

 

记住可以在YAML文档中定义spring.profiles来决定在配置中包含特定的文档。更多细节请看70.7小节,“根据环境更改配置”。

25.2 Programmatically setting profiles

You can programmatically set active profiles by calling SpringApplication.setAdditionalProfiles(…​) before your application runs. It is also possible to activate profiles using Spring’s ConfigurableEnvironment interface.

你可以在你的应用运行之前以编程的方式调用SpringApplication.setAdditionalProfiles(…​)来设置激活的profiles。也可以使用Spring的ConfigurableEnvironment接口来激活profiles。

25.3 Profile-specific configuration files

Profile-specific variants of both application.properties (or application.yml) and files referenced via @ConfigurationProperties are considered as files are loaded. See Section 24.4, “Profile-specific properties” for details.

application.properties(或application.yml)和通过@ConfigurationProperties引用的文件的特定profiles变种都被当做文件进行加载。更多细节请看24.4小节,“Profile-specific properties”。

如果有收获,可以请我喝杯咖啡!